package com.zq.it.action.admin;

import java.util.List;

import javax.annotation.Resource;

import com.zq.it.action.BaseAction;
import com.zq.it.dao.common.PageConfig;
import com.zq.it.pojo.TClass;
import com.zq.it.pojo.Teacher;
import com.zq.it.pojo.exam.data.ObjectData;
import com.zq.it.service.admin.ClassService;
import com.zq.it.tool.Constant;
import com.zq.it.tool.EtyLsName;
import com.zq.it.tool.FileName;
import com.zq.it.tool.FileType;
import com.zq.it.tool.HeadTitle;

import net.sf.json.JSONArray;

public class ClassAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private ObjectData tc;
	@Resource
	private ClassService classService;
	public void searchClassList() {
		List list = classService.getAllClass();
		uploadList(EtyLsName.CLASS_LS.name(), list);
		print(JSONArray
				.fromObject(list,
						com.zq.it.tool.Constant.getJcf(new String[]{"academy"}))
				.toString(), "UTF-8", "APPLICATION/JSON");
	}
	public void searchClassById() {
		if (classService.getClassById(getId()).size() > 0)
			print("false", "UTF-8", "text/text");
	}
	public void modifyClassPo() {
		classService.renewalClass(tclass);
		print("<script>parent.closeWindow('已修改！');</script>", "UTF-8",
				"text/html");
	}
	public void addClassPo() {
		if (tclass.getMajor().getMajorId() == 0)
			tclass.setMajor(null);
		classService.addClass(tclass);
		print("<script>parent.closeWindow('已添加');</script>", "UTF-8",
				"text/html");

	}
	public String getClassList() {
		Teacher teacher = (Teacher) getHttpSession()
				.getAttribute(Constant.TEACHER);
		PageConfig config = new PageConfig();
		config.setHql("from TClass as t");
		config.setCountHql("select count(*) from TClass");
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		tc = classService.getListByPage(config);
		return "classList";
	}
	
	public void searchMajorNameAndId() {
		print(toJSON(classService.getMajorNameAndId()).toString(), "UTF-8",
				"APPLICATION/JSON");
	}
	public void importClassList() {
		String msgStr = "";
		List<TClass> list = classService.importClassList(getUpload());
		if (list.size() > 0) {
			getRequest().getSession().setAttribute("cexistList", list);
			msgStr = "existList";
		} else {
			msgStr = "已导入成功!";
		}
		print("<script>parent.importBackResult('" + msgStr
				+ "','webpage/adminpage/class/cexistClass.jsp');</script>",
				"UTF-8", "TEXT/HTML");
	}
	public void deleteClassGroups() {
		classService.removeAllClass(classGroupId);
		print("已删除" + classGroupId.length + "条记录！", "UTF-8", "text/html");
	}
	public void exportClassList() {
		download(
				classService.exportClassList(HeadTitle.CN_CLASSHEAD.getArray(),
						(List<TClass>) getHttpSession()
								.getAttribute(EtyLsName.CLASS_LS.name())),
				FileName.CLASS_LIST_NAME.toString() + FileType.XLSX);
	}
	
	public void searctClassListBySQL() {
		StringBuilder sb = new StringBuilder("from TClass as tc where 0=0");
		if (getId() != null)
			sb.append(" and tc.classId=" + getId());
		if (getName() != null && !"".equals(getName()))
			sb.append(" and tc.className like '%" + getName() + "%'");
		List list = classService.getClassListBySql(sb.toString());
		uploadList(EtyLsName.CLASS_LS.name(), list);
		print(JSONArray.fromObject(list).toString(), "UTF-8",
				"APPLICATION/JSON");
	}
	public TClass getTclass() {
		return tclass;
	}
	public void setTclass(TClass tclass) {
		this.tclass = tclass;
	}

	public int[] getClassGroupId() {
		return classGroupId;
	}

	public void setClassGroupId(int[] classGroupId) {
		this.classGroupId = classGroupId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public int getMajorId() {
		return majorId;
	}
	public void setMajorId(int majorId) {
		this.majorId = majorId;
	}
	// private int id;
	private String name;
	private int majorId;
	private int[] classGroupId;
	private TClass tclass;

}
